
package pe.com.yugi.DAO.Impl;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import pe.com.yugi.DAO.CartaDAO;
import pe.com.yugi.Dominio.Carta;
import pe.com.yugi.Util.HibernateUtil;


public class CartaDAOImpl implements CartaDAO, Serializable{

    @Override
    public boolean agregar(Carta c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public boolean modificar(Carta c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.update(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public boolean eliminar(int idCarta) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Carta> listarCartas() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Carta order by nombre asc");
        List<Carta> list = query.list();
        for(Carta c : list){
            Hibernate.initialize(c.getPlaneta());
            Hibernate.initialize(c.getTipoCarta());
        }
        return list;
    }

    @Override
    public Carta obtenerCarta(int idCarta) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Carta where idCarta="+idCarta+" order by nombre asc");
        Carta list = (Carta) query.uniqueResult();
        Hibernate.initialize(list.getPlaneta());
        Hibernate.initialize(list.getTipoCarta());
        return list;
    }
    
}
